package com.hzlj.position.locate.differentiation.sysCheck.traceCheck.cross;

import com.hzlj.common.message.common.dto.message.MessageNoticeResultDTO;
import com.hzlj.position.config.common.enums.ChannelType;
import com.hzlj.position.locate.common.common.PositionSimpleDTO;
import com.hzlj.position.locate.common.dto.differentiationData.DifferentiationDataQueryResultDTO;
import com.hzlj.position.locate.common.dto.position.PositionQueryResultDTO;
import com.hzlj.position.locate.common.enums.PositionSysCheck;
import com.hzlj.position.locate.common.model.Position;
import com.hzlj.position.locate.differentiation.common.PositionSysCheckHandle;
import com.hzlj.position.locate.differentiation.sysCheck.traceCheck.TraceSysCheckCommon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 广东
 */
@Slf4j
@Service
public class CrossSysCheckGd extends CrossSysCheck {
    @Resource
    private TraceSysCheckCommon traceSysCheckCommon;
    @Resource
    private PositionSysCheckHandle positionSysCheckHandle;

    /**
     * 第一步：通知矫正人员
     */
    @Override
    public void noticeJzry(List<Position> positions,
                           Map<String, MessageNoticeResultDTO> notices) {
        traceSysCheckCommon.noticeJzry(positions, notices, this);
    }


    /**
     * 第二步：矫正人员上报
     *
     * @param report         上报位置
     * @param originPosition 原始记录
     */
    public void updateReportResult(String jzId,
                                   String jgCode,
                                   PositionSimpleDTO report,
                                   PositionSimpleDTO originPosition) {
        traceSysCheckCommon.updateReportResult(jzId, jgCode, report, originPosition, this);
    }


    /**
     * 处理第二步的信息
     */
    @Override
    public void handleDifferentiationData(DifferentiationDataQueryResultDTO data) {
        traceSysCheckCommon.handleDifferentiationData(data, this, (normal, reportKey) -> {
            //核查记录的处理
            if (normal) {
                positionSysCheckHandle.handleSysCheck(reportKey, PositionSysCheck.SUCCESS);
            } else {
                positionSysCheckHandle.handleAbn(reportKey, this.getDifferentiationConfigExt().getTraceCheck().getAbnRemark());
            }
        });
    }

    /**
     * 第三步：发送未上报结果
     */
    @Override
    public void updateNoReportResult(PositionQueryResultDTO position,
                                     MessageNoticeResultDTO notice) {
        traceSysCheckCommon.updateNoReportResult(position, notice, this);
    }

    @Override
    public List<Position> filterPosition(List<Position> positions) {
        return positions.stream()
                .filter(e -> e.getChannelType().equals(ChannelType.MOBILE.getCode()))
                .collect(Collectors.toList());
    }
}
